//-----------------------------------------------------------------------
// <copyright file="a_Email_PermissionDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_Email_PermissionDAOBase
    {
        #region Common methods
        public virtual a_Email_Permission Createa_Email_PermissionFromReader(IDataReader reader)
        {
            a_Email_Permission item = new a_Email_Permission();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (int)reader["UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("MTmpType"))) item.MTmpType = (string)reader["MTmpType"];
                if (!reader.IsDBNull(reader.GetOrdinal("Permission"))) item.Permission = (bool)reader["Permission"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Email_Permission methods
            
        public virtual void Createa_Email_Permission(a_Email_Permission a_Email_Permission)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Email_Permissions_ins");
                
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Email_Permission.UserID);
                database.AddInParameter(dbCommand, "@MTmpType", DbType.AnsiString, a_Email_Permission.MTmpType);
                database.AddInParameter(dbCommand, "@Permission", DbType.Boolean, a_Email_Permission.Permission);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Email_Permission methods
        
        public virtual void Updatea_Email_Permission(a_Email_Permission a_Email_Permission)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Email_Permissions_upd");            
                
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Email_Permission.UserID);
                database.AddInParameter(dbCommand, "@MTmpType", DbType.AnsiString, a_Email_Permission.MTmpType);
                database.AddInParameter(dbCommand, "@Permission", DbType.Boolean, a_Email_Permission.Permission);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Email_Permission methods
        public virtual void Deletea_Email_Permission(int userID, string mTmpType)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Email_Permissions_del");
                
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, userID);
                database.AddInParameter(dbCommand, "@MTmpType", DbType.AnsiString, mTmpType);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Email_Permission methods
        
        public virtual a_Email_Permission Geta_Email_Permission(int userID, string mTmpType)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Email_Permissions_sel");
                
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, userID);
                database.AddInParameter(dbCommand, "@MTmpType", DbType.AnsiString, mTmpType);
                
                a_Email_Permission a_Email_Permission =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Email_Permission = Createa_Email_PermissionFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Email_Permission;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_Email_PermissionList methods
        public virtual a_Email_PermissionCollection Geta_Email_PermissionList(a_Email_PermissionColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Email_Permissions_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_Email_PermissionCollection a_Email_PermissionCollection = new a_Email_PermissionCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Email_Permission a_Email_Permission = Createa_Email_PermissionFromReader(reader);
                        a_Email_PermissionCollection.Add(a_Email_Permission);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_Email_PermissionCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_Email_PermissionCollection Geta_Email_PermissionList(a_Email_PermissionColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_Email_PermissionList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}